//index.js
//获取应用实例
var util = require('../../utils/util.js');
var Promise = require('../../utils/bluebird.min.js');
var app = getApp()
Page({
  data: {
    starScore:7,
    userInfo: {},
    theratersMovieList:[],
    comingMovieList:[],
    topMovieList:[]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goToMoreMovie:function(event){
    // console.log(event)
    var moreMovieUrl = event.currentTarget.dataset.movieUrl;
    wx.navigateTo({
      url: '../moviemore/moviemore?url=' + moreMovieUrl
    })
  },
  showImg:function(event){
    var imgSrc = event.currentTarget.dataset.imgSrc;
    //图片预览
    wx.previewImage({
      current:imgSrc,
      urls: [imgSrc]
    })
    //文件下载
    wx.downloadFile({
      url:imgSrc,
      success:function (res){
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success:function(data){
            console.log(data);
          },
          fail:function(err){
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny"){
              console.log("用户一开始拒绝了，我们想再次发起授权")
              // wx.authorize({
              //   scope: 'scope.writePhotosAlbum',
              //   success(successdata) {
              //     console.log('授权成功')
              //   },
              //   fail(faildata) {
              //     console.log('授权失败')
              //     console.log(faildata)
              //   }
              // })
              console.log('打开设置窗口')
              wx.openSetting({
                success(settingdata){
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']){
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  }else{
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  // 回调函数的方式
  // getMovieList: function (url,title, requestData, successCallBack) {
  //   var self = this;
  //   wx.request({
  //     url: util.serverUrlFactory(url),
  //     method: 'GET',
  //     header: {
  //       "content-type": "json"
  //     },
  //     data: requestData,
  //     success: function (res) {
  //       console.log(res);
  //       res.data.title = title;
  //       successCallBack(res.data);
  //     },
  //     fail: function (err) {
  //       console.log(err);
  //     }
  //   })
  // },

  //移动端到util.js
  // getMovieList: function (url, title, requestData) {
  //   var self = this;
  //   return new Promise((resolve, reject) => {   
  //     wx.request({
  //       url: util.serverUrlFactory(url),
  //       method: 'GET',
  //       header: {
  //         "content-type": "json"
  //       },
  //       data: requestData,
  //       success: function (res) {
  //         console.log(res);
  //         res.data.title = title;
  //         resolve(res.data);
  //       },
  //       fail: function (err) {
  //         reject(err);
  //         console.log(err);
  //       }
  //     })   
  //   })
  // },
  // movieDataFactory:function(data){
  //   var minData = [];
  //   for (var key in data.subjects){
  //     minData.push({
  //       medium: data.subjects[key].images.medium,
  //       title: data.subjects[key].title,
  //       average: data.subjects[key].rating.average
  //     })
  //   }
  //   return {
  //     title:data.title,
  //     subjects:minData
  //   };
  // },
  onLoad: function () {
    console.log('onLoad')
    var that = this;
    wx.showToast({
      title:'加载中',
      icon:'loading',
      mask:true
    })
    util.getMovieList("/v2/movie/in_theaters", "近期上映", { count: 3 }).then((theratersMovieListdata)=>{
      util.getMovieList("/v2/movie/coming_soon", "即将上映", { count: 3 }).then((comingMovieListdata)=>{
        util.getMovieList("/v2/movie/top250", "热门电影", { count: 3 }).then((topMovieListdata) => {
           that.setData({
             theratersMovieList: util.movieDataFactory(theratersMovieListdata),
             comingMovieList: util.movieDataFactory(comingMovieListdata),
             topMovieList: util.movieDataFactory(topMovieListdata)
          });
          wx.hideToast();
        })
      })
    })
    // 回调函数的方式
    // that.getMovieList("/v2/movie/in_theaters", "近期上映", { count: 3 }, function (theratersMovieListdata){
    //   that.getMovieList("/v2/movie/coming_soon", "即将上映", { count: 3 }, function (comingMovieListdata) {
    //     that.getMovieList("/v2/movie/top250", "热门电影", { count: 3 }, function (topMovieListdata) {
    //       that.setData({
    //         theratersMovieList: that.movieDataFactory(theratersMovieListdata),
    //         comingMovieList: that.movieDataFactory(comingMovieListdata),
    //         topMovieList: that.movieDataFactory(topMovieListdata)
    //       });
    //     });
    //   });
    // }); 
  }
})
